--- 智迟技能
--- “掎势”转换后，若转换序号为你已损失体力值，你可以回复一点体力并向前转换一次；
--- 你受到伤害后，若“掎势”序号为你体力值，你可以摸两张牌并分配两张牌，然后向后转换一次。
local zhichi = fk.CreateSkill {
  name = "lvshi__zhichi",
}

local DIY = require "packages/diy_utility/diy_utility"

-- 监听卡牌使用完成，这是掎势使用后状态可能改变的合理时机
-- 当掎势技能使用的卡牌结算完成后，检查状态是否匹配
zhichi:addEffect(fk.CardUseFinished, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return data.card and data.card.skillName == "lvshi__jishi_viewas&" and
    player:hasSkill(zhichi.name) and player:isWounded() and target:getMark("jishi_from") == player.id
    and DIY.getSwitchState(player, "lvshi__jishi") == player:getLostHp()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recover{ who = player, num = 1, skillName = zhichi.name }
    DIY.changeSwitchState(player, "lvshi__jishi", false)
    room:sendLog{ type = "#lvshi__zhichi-forward", from = player.id, arg = "lvshi__jishi", toast = true }
  end,
})

zhichi:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zhichi.name) and DIY.getSwitchState(player, "lvshi__jishi") == player.hp
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, zhichi.name)
    
    -- 获取手牌用于分配
    local hand_cards = player:getCardIds("h")
    if #hand_cards >= 1 then
      -- 使用askToYiji分配牌
      local distribution_result = room:askToYiji(player, {
        cards = hand_cards,
        min_num = 1,
        max_num = 1,
        skill_name = zhichi.name,
        prompt = "#lvshi__zhichi-give",
      })
      
      -- 如果成功分配了牌，则向后转换
      if distribution_result then
        DIY.changeSwitchState(player, "lvshi__jishi", true)
        room:sendLog{ type = "#lvshi__zhichi-backward", from = player.id, arg = "lvshi__jishi", toast = true}
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["lvshi__zhichi"] = "智迟",
  ["#lvshi__zhichi"] = "智迟", 
  ["$lvshi__zhichi"] = "",
  [":lvshi__zhichi"] = "“掎势”转换后，若转换序号为你已损失体力值，你可回复1点体力并将“掎势”向前转换一次；你受到伤害后，若“掎势”序号为你体力值，你可摸一张牌并分配一张牌，然后将“掎势”向后转换一次。",
  ["#lvshi__zhichi-recover"] = "智迟：是否回复体力并向前转换？",
  ["#lvshi__zhichi-draw"] = "智迟：是否摸牌分配并向后转换？",
  ["#lvshi__zhichi-give"] = "选择一张牌分配",
  ["#lvshi__zhichi-forward"] = "%arg 向前转换",
  ["#lvshi__zhichi-backward"] = "%arg 向后转换",
  ["~lvshi__zhichi"] = "",
}

return zhichi